﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Deployment.WindowsInstaller;
using System.Diagnostics;

namespace CitrixPvsSetup_RegisterDll
{
    public class CustomActions
    {
        [CustomAction]
        public static ActionResult RegisterPvsDll(Session session)
        {
            // Specify the path to the InstallUtil tool and make sure that it exists
            String InstallUtilPath = Environment.GetEnvironmentVariable("windir") + @"\Microsoft.NET\Framework64\v2.0.50727\installutil.exe";
            if (System.IO.File.Exists(InstallUtilPath) == false)
            {
                String ErrMsg = InstallUtilPath + " not found. DLL not registered. DLL will need to be registered manually";
                Record rec = new Record();
                rec.FormatString = ErrMsg;
                    
                session.Message(InstallMessage.Error,rec);
                session.Log(ErrMsg);
                return ActionResult.NotExecuted; 
            }

            // Test the existence of the McliPsSnapIn.dll file 
            String McliPsSnapInDll = @"C:\Program Files\Citrix\Provisioning Services Console\McliPSSnapIn.dll";
            if (System.IO.File.Exists(McliPsSnapInDll) == false)
            {
                String ErrMsg = McliPsSnapInDll + " not found. DLL not registered. DLL will need to be registered manually";
                Record rec = new Record();
                rec.FormatString = ErrMsg;

                session.Message(InstallMessage.Error, rec);
                session.Log(ErrMsg);
                return ActionResult.NotExecuted;
            }

            // Configure the parameters for th exe
            ProcessStartInfo RegisterDllParams = new ProcessStartInfo();
            RegisterDllParams.CreateNoWindow = false;
            RegisterDllParams.UseShellExecute = false;
            RegisterDllParams.RedirectStandardError = true;
            RegisterDllParams.RedirectStandardOutput = true;
            RegisterDllParams.FileName = InstallUtilPath;
            RegisterDllParams.Arguments = @"""" + McliPsSnapInDll + @"""";

            // Execute the registration and capture the result
            Process RegisterDll;
            try
            {
                RegisterDll = Process.Start(RegisterDllParams);
                RegisterDll.WaitForExit();
            }
            catch (Exception e)
            {
                String ErrMsg = "Exception running InstallUtil. Error: " + e.Message + ". DLL will need to be registered manually";
                Record rec = new Record();
                rec.FormatString = ErrMsg;
                session.Message(InstallMessage.Error, rec);
                session.Log(ErrMsg);
                return ActionResult.Failure;
            }

            // Evaluate the return code
            if (RegisterDll.ExitCode != 0)
            {
                String ErrMsg = RegisterDll.StandardOutput.ReadToEnd();
                Record rec = new Record();
                rec.FormatString = ErrMsg;
                session.Message(InstallMessage.Error, rec);
                session.Log(ErrMsg);
                return ActionResult.Failure;
            }
            else
            {
                return ActionResult.Success;
            }

            
        }
    }
}
